package com.tuniu.agents.tools.flights.international.oneway;

import com.fasterxml.jackson.annotation.JsonClassDescription;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyDescription;
import com.tuniu.agents.advisor.ChatEnvironmentMemory;
import com.tuniu.agents.tools.flights.FlightProperties;
import com.tuniu.agents.tools.flights.utils.AirportQueryUtils;
import com.tuniu.agents.util.HttpManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.ai.chat.model.ToolContext;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.function.BiFunction;

public class IntlFlightLowPriceQueryTool implements BiFunction<IntlFlightLowPriceQueryTool.Request, ToolContext, IntlFlightLowPriceQueryTool.Response> {

    public static final String TOOLNAME = "IntlFlightLowPriceQueryTool";

    private static final Logger logger = LoggerFactory.getLogger(IntlFlightLowPriceQueryTool.class);

    private final String flightApiUrl;
    private final String approachApiUrl;
    private final String flightNearGoQueryToolUrl;
    private final String flightNearBackQueryToolUrl;

    @Autowired(required = false)
    private ChatEnvironmentMemory chatEnvironmentMemory;

    @Autowired
    private HttpManager httpManager;

    public IntlFlightLowPriceQueryTool(FlightProperties properties) {
        this.flightApiUrl = properties.getIntlFlightLowPriceQueryToolUrl();
        this.approachApiUrl = properties.getApproachQueryToolUrl();
        this.flightNearGoQueryToolUrl = properties.getIntlFlightNearGoQueryToolUrl();
        this.flightNearBackQueryToolUrl = properties.getIntlFlightNearBackQueryToolUrl();
    }

    @Override
    public Response apply(Request request, ToolContext context) {
        Map<String,Object> map = context.getContext();
        logger.info("IntLowPriceQueryTool请求入参:{}", request);
        Response post;
        List<String> cityList = new ArrayList<>();
        cityList.add(request.departureCityName);
        cityList.add(request.arrivalCityName);
        Map<String, Boolean> mapCity = AirportQueryUtils.batchCheckAirports(httpManager, approachApiUrl, cityList);
        if (mapCity.get(request.departureCityName)){
            post = httpManager.post(flightNearGoQueryToolUrl, request, Response.class);
        } else if (mapCity.get(request.arrivalCityName)){
            post = httpManager.post(flightNearBackQueryToolUrl, request, Response.class);
        } else {
            post = httpManager.post(flightApiUrl, request, Response.class);
        }
        if (post.queryId != null){
            Map<String,Object> hashMap = chatEnvironmentMemory.get((String)map.get("conversation_id"));
            hashMap.put("queryId", post.queryId);
            chatEnvironmentMemory.add((String)map.get("conversation_id"), hashMap);
        }
        logger.info("flightApiUrl:{}", context);
        return post;
    }

    @JsonClassDescription("国际机票单程列表查询入参")
    public record Request(
            @JsonProperty(value = "departureCityName") @JsonPropertyDescription("出发城市") String departureCityName,
            @JsonProperty(value = "arrivalCityName") @JsonPropertyDescription("到达城市") String arrivalCityName,
            @JsonProperty(value = "departureDate") @JsonPropertyDescription("出发日期") String departureDate) {
    }

    @JsonClassDescription("国际机票单程列表查询返参")
    public record Response(@JsonProperty(value = "queryId") @JsonPropertyDescription("查询ID") String queryId,
            @JsonProperty(value = "data") @JsonPropertyDescription("返回的国际航班数据") List<FlightItem> data) {

    }

    @JsonClassDescription("国际航班列表")
    public record FlightItem(
            @JsonProperty(value = "airlineCompany") @JsonPropertyDescription("航司") String airlineCompany,
            @JsonProperty(value = "arrivalAirport") @JsonPropertyDescription("到达机场") String arrivalAirport,
            @JsonProperty(required = true, value = "arrivalTime") @JsonPropertyDescription("到达时间") String arrivalTime,
            @JsonProperty(value = "cabinClass") @JsonPropertyDescription("舱等") String cabinClass,
            @JsonProperty(value = "craftType") @JsonPropertyDescription("机型") String craftType,
            @JsonProperty(value = "departureAirport") @JsonPropertyDescription("出发机场") String departureAirport,
            @JsonProperty(required = true, value = "departureTime") @JsonPropertyDescription("出发时间") String departureTime,
            @JsonProperty(required = true, value = "flightNumber") @JsonPropertyDescription("国际航班号") String flightNumber,
            @JsonProperty(required = true, value = "priceWithTax") @JsonPropertyDescription("成人价格") String priceWithTax,
            @JsonProperty(value = "remainingSeats") @JsonPropertyDescription("剩余位置") String remainingSeats,
            @JsonProperty(value = "type") @JsonPropertyDescription("航班类型：直飞，中转") String type,
            @JsonProperty(value = "totalDuration") @JsonPropertyDescription("总时长") String totalDuration,
            @JsonProperty(value = "stopPoint") @JsonPropertyDescription("经停点") String stopPoint,
            @JsonProperty(value = "transferPoint") @JsonPropertyDescription("中转地") String transferPoint,
            @JsonProperty(value = "stopTime") @JsonPropertyDescription("经停") String stopTime,
            @JsonProperty(value = "transferTime") @JsonPropertyDescription("中转时长") String transferTime) {
    }
}
